package com.csaba.util;

import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.Currency;
import java.util.HashMap;
import java.util.Map;

public class Formatters
{
	////////////// DATE

	private static final ThreadLocal<DateFormat> dateFactory = new ThreadLocal<DateFormat>()
	{
		@Override
		protected DateFormat initialValue()
		{
			return DateFormat.getDateInstance(DateFormat.SHORT);
		}
	};

	public final static DateFormat getShortDateFormat()
	{
		return dateFactory.get();
	}

	////////// CURRENCY

	private final static Map<Currency, ThreadLocal<NumberFormat>> currencyFormatters = new HashMap<Currency, ThreadLocal<NumberFormat>>();

	public final static NumberFormat getCurrencyFormat( final Currency currency )
	{
		if ( !currencyFormatters.containsKey(currency) )
		{
			currencyFormatters.put(currency, new ThreadLocal<NumberFormat>()
			{
				@Override
				protected NumberFormat initialValue()
				{
					final NumberFormat format = NumberFormat.getNumberInstance();
					format.setMinimumFractionDigits(currency.getDefaultFractionDigits());
					format.setMaximumFractionDigits(currency.getDefaultFractionDigits());
					return format;
				}
			});
		}

		return currencyFormatters.get(currency).get();
	}

	private Formatters()
	{
		// private constructor to avoid instantiating this class
	}

}
